/**
 * @author zhounan 
 * @date 2024年11月22日
 * @version V1.0
 */
package org.zn.etl.misc;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import lombok.extern.slf4j.Slf4j;

/**
 * @author zhounan 
 * @date 2024年11月22日
 * @version V1.0
 */
@Slf4j
public class ErrorMessageFilterHelper {

	private	Map<String, Pattern> patternMap;

	private static ErrorMessageFilterHelper self;
	
	static {
		
		self=new ErrorMessageFilterHelper();
		Properties properties = new Properties();
		try {
			properties.load(ErrorMessageFilterHelper.class.getClassLoader().getResourceAsStream("ErrorMessageFilter.properties"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		self.patternMap=new HashMap<>();
		//self.patternMap.put("duplicate key", Pattern.compile("^.+key.+already exists.$"));
		for (Object key : properties.keySet()) {
			
			log.info("patternMap put: {},{}",key.toString(),properties.getProperty(key.toString()));
			
			self.patternMap.put(key.toString(), Pattern.compile(properties.getProperty(key.toString())));
			
		}
		
	}
	
	
	
	public static ErrorMessageFilterHelper getFilter()  {
		
		return self;
	}
	
	
	public  boolean FilterMessage(String errorMessage) {

		for (Entry<String, Pattern> ent : patternMap.entrySet()) {
			
			Pattern patten= ent.getValue();
			
			Matcher match=patten.matcher(errorMessage);
			
			if(match.find()) {
				return false;
			}
			
		}
		
		return true;
		
	}
	

	public static void main(String[] args) throws IOException {
		
		Boolean flag= ErrorMessageFilterHelper.getFilter().FilterMessage("ERROR: duplicate key value violates unique constraint \"t_querylog_pkey\" 详细:key  ");
		
		System.out.println(flag); 
	}
	
	
	
	
	
	
}
